<template>
	<view class="content">
		<u-swiper
			:list="imageList"
			indicator
			indicatorMode="line"
			circular
			height="180px"
    >
		</u-swiper>
		<view class="uni-padding-wrap uni-common-mt" style="margin-top: 40rpx">
      <view class="uni-flex uni-row" style="flex-wrap: wrap;">
				<view class="flex-item">
					<button class="custom-style" @click="getMessage">打卡</button>
				</view>
      </view>
    </view>
		<information></information>
		<!-- 打卡前是否已绑定学校 -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import information from './information.vue'
	export default {
		components: {
			information
		},
		onShow() {
			uni.getStorage({
				key: '218002',
				success: ((res) => {
					console.log(res);
				  this.iv = res.data.resTwo.iv;
					this.encryptedData = res.data.resTwo.encryptedData;
				})
			})
		},
		data() {
			return {
				workList: [
					{icon:'../../static/icons/home.png',title:'考勤记录'},
					{icon:'../../static/icons/home.png',title:'考勤明细'},
					{icon:'../../static/icons/home.png',title:'健康上报'}
				],
				imageList: [
					'https://cdn.uviewui.com/uview/swiper/swiper2.png',
					'https://cdn.uviewui.com/uview/swiper/swiper1.png',
				],
				//用户的openid和头像等信息
				details: {},
				//登录获取的code
				code: '',
				//用code从微信的后端拿到了openid,用户在小程序的唯一id
				openid: '',
				//用openid去自己的后端拿到的学生信息里的id
				id: undefined,
				//微信登录获取的iv
				iv: '',
				//微信登录获取的encryptedData
				encryptedData: '',
				//打卡前是否已绑定学校弹出框
				toast: {
						type: 'error',
						icon: false,
						message: "请先绑定学校",
					},
			}
		},
		methods: {
			getMessage(){
				let that_ = this;
				let session_key = null;
				//如果学生已经登录过了
				if (that_.iv != '') {
					uni.login({
							provider: 'weixin',
							success: function(loginRes) {
								if (loginRes.errMsg == "login:ok") {
									//拿到用户的code
									that_.code = loginRes.code;
									//去微信后端用code等信息拿到用户的openid
									uni.request({  
									  url: 'https://api.weixin.qq.com/sns/jscode2session',  
										method:'GET',  
										data :{
												"appid": "wxf6f4788558701800",
												"secret": "b264e649a93fc84524f87dd15e6b8934",
												"js_code": that_.code,
												"grant_type":"authorization_code"
											},  
										success: (res) => {
											//拿到用户的openid
											that_.openid = res.data.openid
											//console.log(that_.openid);
											//去自己的后端用openid换取用户的学生信息表
											uni.request({  
												url: 'http://localhost:8123/user/log',  
												method:'POST',  
												data : that_.openid,
												success: (cts) => {  
													//console.log(cts.data);
													if (cts.data.code == 200) {
														that_.id = cts.data.data.id;
														uni.setStorage({
															key:'studentId',
															data: cts.data.data.id,
															success: function(){
																console.log("success");
															}
														})
														//如果学生进行了学校绑定
														uni.navigateTo({
															url: 'clockMessage/index?id='+that_.id,
															success: res =>{console.log(res)},
															fail:err=>{
																console.log(err)
															}
													})
													} else {
														//学生没进行绑定
														that_.$refs.uToast.show({
															...that_.toast,
														})
													}
										    }  
								      });
										}  
								  }); 
								}
							}
						});
				} else {
					//获取用户微信信息
					uni.getUserProfile({
						desc: "授权登录",
						lang: "zh_CN",
						success: function(resTwo) {
							that_.details.resTwo = resTwo;
							that_.iv = resTwo.iv;
							that_.encryptedData = resTwo.encryptedData;
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									if (loginRes.errMsg == "login:ok") {
										//拿到用户的code
										that_.code = loginRes.code;
										//去微信后端用code等信息拿到用户的openid
										uni.request({  
											url: 'https://api.weixin.qq.com/sns/jscode2session',  
											method:'GET',  
											data :{
													"appid": "wxf6f4788558701800",
													"secret": "b264e649a93fc84524f87dd15e6b8934",
													"js_code": that_.code,
													"grant_type":"authorization_code"
												},  
											success: (res) => {
												//拿到用户的openid
												that_.openid = res.data.openid;
												that_.details.openid = res.data.openid;
												console.log(that_.openid);
												uni.setStorage({
													key: '218002',
													data: that_.details,
													success: function () {
														console.log('success');
													}
												});
												//去自己的后端用openid换取用户的学生信息表
												uni.request({  
													url: 'http://localhost:8123/user/log',  
													method:'POST',  
													data : that_.openid,
													success: (cts) => {  
														console.log(cts.data);
														if (cts.data.code == 200) {
															//如果学生进行了学校绑定
															that_.id = cts.data.data.id;
															uni.setStorage({
																key:'studentId',
																data: cts.data.data.id,
																success: function(){
																	console.log("success");
																}
															})
															uni.navigateTo({
																url: 'clockMessage/index?id='+that_.id,
																success: res =>{console.log(res)},
																fail:err=>{
																	console.log(err)
																}
														  })
														} else {
															//学生没进行绑定
															that_.$refs.uToast.show({
																...that_.toast,
															})
														}
													}  
												});
											}  
										}); 
									}
								}
							});
						}
					})
				}
			},
		}
	}
</script>

<style scoped>
.content {
	margin: 0;
	padding: 0;
	position: relative;
}
.btn {
	margin: 0 auto;
	padding: 7px;
	width: 20%;
	height: 20%;
	border-radius: 50%;
	color: white;
}
.flex-item {
	width: 100%;
	height: 200rpx;
	text-align: center;
	line-height: 200rpx;
	background-color: rgba(255, 255, 255, 0);
	display: flex;
	align-items: center;
	justify-content: center;
	flex-direction: column;
}
.uni-padding-wrap{
	width: 100%;
	padding: 0rpx;
	margin: 0rpx;
}
.custom-style {
	color: rgb(255, 255, 255);
	background-image: linear-gradient(to right, red , rgb(255, 217, 0));
	width: 180rpx;
	height: 180rpx;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
  flex-wrap: nowrap;
}
</style>
